﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using Autodesk.Revit.DB;

namespace YJKRevitFamilyDataBase
{
	public class SqliteFamilyParamTable : IFamilyParamTable
	{
		public SqliteFamilyParamTable(FamilyLibDatabase db)
		{
			this.mDatabase = db;
			if (this.mDatabase.Connection.State == ConnectionState.Closed)
			{
				this.mDatabase.Connection.Open();
			}
		}

		public bool Add(DbFamily family, List<DbParamDefinition> pdList, DbTransaction tr)
		{
			bool result;
			try
			{
				foreach (DbParamDefinition dbParamDefinition in pdList)
				{
					int num;
					if (dbParamDefinition.Limit)
					{
						num = 1;
					}
					else
					{
						num = 0;
					}
					if (dbParamDefinition.ParamDefinitionType == ParamDefinitionType.CustomParam)
					{
						this.mDatabase.ExecuteCommand(string.Format("insert into [FamilyParamTable]([FamilyGUID],[ParamName],[ParamId],[CustomName],[ParamOrder],[Unit],[Limit],[ParamType],[BuiltInParameterGroup],[DisplayUnitType],[Description]) values ('{0}','{1}','{2}','{3}','{4}','{5}',{6},'{7}','{8}','{9}','{10}')", new object[]
						{
							family.Guid.ToString(),
							dbParamDefinition.Name,
							"0",
							dbParamDefinition.CustomName,
							dbParamDefinition.Order,
							dbParamDefinition.Unit,
							num,
							dbParamDefinition.ParameterType,
							dbParamDefinition.BuiltInParameterGroup,
							dbParamDefinition.DisplayUnitType,
							dbParamDefinition.Description
						}), tr);
					}
					else if (dbParamDefinition.ParamDefinitionType == ParamDefinitionType.TypeParam)
					{
						this.mDatabase.ExecuteCommand(string.Format("insert into [FamilyParamTable]([FamilyGUID],[ParamName],[ParamId],[CustomName],[ParamOrder],[Unit],[Limit],[ParamType],[BuiltInParameterGroup],[DisplayUnitType],[Description]) values ('{0}','{1}','{2}','{3}','{4}','{5}',{6},'{7}','{8}','{9}','{10}')", new object[]
						{
							family.Guid.ToString(),
							dbParamDefinition.Name,
							"1",
							dbParamDefinition.CustomName,
							dbParamDefinition.Order,
							dbParamDefinition.Unit,
							num,
							dbParamDefinition.ParameterType,
							dbParamDefinition.BuiltInParameterGroup,
							dbParamDefinition.DisplayUnitType,
							dbParamDefinition.Description
						}), tr);
					}
					else if (dbParamDefinition.ParamDefinitionType == ParamDefinitionType.InstanceParam)
					{
						this.mDatabase.ExecuteCommand(string.Format("insert into [FamilyParamTable]([FamilyGUID],[ParamName],[ParamId],[CustomName],[ParamOrder],[Unit],[Limit],[ParamType],[BuiltInParameterGroup],[Description]) values ('{0}','{1}','{2}','{3}','{4}','{5}',{6},'{7}','{8}','{9}')", new object[]
						{
							family.Guid.ToString(),
							dbParamDefinition.Name,
							"2",
							dbParamDefinition.CustomName,
							dbParamDefinition.Order,
							dbParamDefinition.Unit,
							num,
							dbParamDefinition.ParameterType,
							dbParamDefinition.BuiltInParameterGroup,
							dbParamDefinition.Description
						}), tr);
					}
				}
				result = true;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public void Remove(DbParamDefinition paramDef, DbFamily family)
		{
			try
			{
				this.mDatabase.ExecuteCommand(string.Concat(new string[]
				{
					"delete from [FamilyParamTable] where [FamilyGUID]='",
					family.Guid.ToString(),
					"' and [ParamName]='",
					paramDef.Name,
					"'"
				}));
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		public void Remove(DbFamily family, DbTransaction tr)
		{
			try
			{
				this.mDatabase.ExecuteCommand("delete from [FamilyParamTable] where [FamilyGUID]='" + family.Guid.ToString() + "'", tr);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		public DbParamDefinition this[DbFamily family, string pName]
		{
			get
			{
				DbParamDefinition result;
				try
				{
					DbParamDefinition dbParamDefinition = null;
					DataTable dataTable = this.mDatabase.ExecQuery(string.Format("select [ParamName],[ParamType],[ParamId],[ParamOrder],[Limit],[Description],[CustomName],[Unit],[BuiltInParameterGroup],[DisplayUnitType] from [FamilyParamTable] where [ParamName]='{0}' and [FamilyGUID]='{1}'", pName, family.Guid.ToString()));
					if (dataTable != null && dataTable.Rows.Count > 0)
					{
						string pName2 = dataTable.Rows[0]["ParamName"].ToString().Trim();
						ParamDefinitionType pDefinitionType = (ParamDefinitionType)Convert.ToInt32(dataTable.Rows[0]["ParamId"]);
						string customName = dataTable.Rows[0]["CustomName"].ToString().Trim();
						int order = Convert.ToInt32(dataTable.Rows[0]["ParamOrder"]);
						string unit = dataTable.Rows[0]["Unit"].ToString().Trim();
						bool limit = Convert.ToBoolean(dataTable.Rows[0]["Limit"]);
						ParameterType pType = (ParameterType)Enum.Parse(typeof(ParameterType), dataTable.Rows[0]["ParamType"].ToString());
						BuiltInParameterGroup parameterGroup = (BuiltInParameterGroup)Enum.Parse(typeof(BuiltInParameterGroup), dataTable.Rows[0]["BuiltInParameterGroup"].ToString());
						DisplayUnitType displayUnitType = (DisplayUnitType)Enum.Parse(typeof(DisplayUnitType), dataTable.Rows[0]["DisplayUnitType"].ToString());
						string description = dataTable.Rows[0]["Description"].ToString().Trim();
						dbParamDefinition = new DbParamDefinition(pName2, pType, pDefinitionType, parameterGroup);
						dbParamDefinition.CustomName = customName;
						dbParamDefinition.SetOrder(order);
						dbParamDefinition.Unit = unit;
						dbParamDefinition.Limit = limit;
						dbParamDefinition.DisplayUnitType = displayUnitType;
						dbParamDefinition.Description = description;
					}
					result = dbParamDefinition;
				}
				catch
				{
					result = null;
				}
				return result;
			}
		}

		public List<DbParamDefinition> this[DbFamily family]
		{
			get
			{
				List<DbParamDefinition> list = new List<DbParamDefinition>();
				DataTable dataTable = this.mDatabase.ExecQuery(string.Format("select [ParamName],[ParamType],[ParamId],[CustomName],[ParamOrder],[Unit],[Limit],[BuiltInParameterGroup],[DisplayUnitType],[Description] from [FamilyParamTable] where [FamilyGUID]='{0}'", family.Guid.ToString()));
				if (dataTable != null)
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						string pName = dataTable.Rows[i]["ParamName"].ToString().Trim();
						ParamDefinitionType pDefinitionType = (ParamDefinitionType)Convert.ToInt32(dataTable.Rows[i]["ParamId"]);
						string customName = dataTable.Rows[i]["CustomName"].ToString().Trim();
						int order = Convert.ToInt32(dataTable.Rows[i]["ParamOrder"]);
						string unit = dataTable.Rows[i]["Unit"].ToString().Trim();
						bool limit = Convert.ToBoolean(dataTable.Rows[i]["Limit"]);
						ParameterType pType = (ParameterType)Enum.Parse(typeof(ParameterType), dataTable.Rows[i]["ParamType"].ToString());
						BuiltInParameterGroup parameterGroup = (BuiltInParameterGroup)Enum.Parse(typeof(BuiltInParameterGroup), dataTable.Rows[i]["BuiltInParameterGroup"].ToString());
						DisplayUnitType displayUnitType = (DisplayUnitType)Enum.Parse(typeof(DisplayUnitType), dataTable.Rows[i]["DisplayUnitType"].ToString());
						string description = dataTable.Rows[i]["Description"].ToString().Trim();
						DbParamDefinition dbParamDefinition = new DbParamDefinition(pName, pType, pDefinitionType, parameterGroup);
						dbParamDefinition.CustomName = customName;
						dbParamDefinition.SetOrder(order);
						dbParamDefinition.Unit = unit;
						dbParamDefinition.Limit = limit;
						dbParamDefinition.DisplayUnitType = displayUnitType;
						dbParamDefinition.Description = description;
						list.Add(dbParamDefinition);
					}
				}
				return list;
			}
		}

		private FamilyLibDatabase mDatabase;
	}
}
